package enterprisePerson

import (
	"context"
	"github.com/google/uuid"
	"github.com/spf13/cast"
	"insighttracking/common/enum"
	"insighttracking/common/utils"
	"insighttracking/query/bo"
	"insighttracking/query/dao"
	"net/http"
	"strings"

	"insighttracking/apps/admin/internal/svc"
	"insighttracking/apps/admin/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetEnterprisCustomerePageApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetEnterprisCustomerePageApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetEnterprisCustomerePageApiLogic {
	hostname, _ := os.Hostname()
	return &GetEnterprisCustomerePageApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *GetEnterprisCustomerePageApiLogic) GetEnterpriseCustomerPageApi(req *types.GetEnterprisePersonPageReq) (resp *types.EnterprisePersonPageResp, err error) {
	enterpriseId := cast.ToInt64(l.ctx.Value("enterpriseId"))
	accountType := cast.ToInt64(l.ctx.Value("accountType"))
	if accountType != enum.EnterpriseEnum {
		return &types.EnterprisePersonPageResp{
			Data: make([]types.EnterprisePersonModel, 0),
			PageBaseResp: types.PageBaseResp{
				Total:      0,
				PageSize:   req.PageSize,
				PageNumber: req.PageNumber,
			},
		}, nil
	}

	tx := dao.EnterprisePersonEntity.WithContext(l.ctx).Where(
		dao.EnterprisePersonEntity.EnterpriseID.Eq(enterpriseId),
		dao.RoleEntity.RoleCode.Neq(enum.RoleCode01Enum),
	)
	if req.Name != "" {
		req.Name = strings.ReplaceAll(req.Name, "%", "\\%")
		req.Name = strings.ReplaceAll(req.Name, "_", "\\%")
		tx = tx.Where(dao.EnterprisePersonEntity.Name.Like("%" + req.Name + "%"))
	}
	if req.Mobile != "" {
		tx = tx.Where(dao.EnterprisePersonEntity.Mobile.Eq(req.Mobile))
	}
	if req.Email != "" {
		tx = tx.Where(dao.EnterprisePersonEntity.Email.Eq(req.Email))
	}
	if req.RoleID != "" {
		tx = tx.Where(dao.EnterprisePersonEntity.RoleID.Eq(cast.ToInt64(req.RoleID)))
	}
	if req.Status != "" {
		tx = tx.Where(dao.EnterprisePersonEntity.Status.Eq(cast.ToInt64(req.Status)))
	}
	var enterprisePersonBo = make([]bo.EnterprisePersonBo, 0)
	offset, limit := utils.DbPage(req.PageNumber, req.PageSize)
	count, err := tx.
		Select(
			dao.EnterprisePersonEntity.ALL,
			dao.RoleEntity.Name.As("RoleName"),
			dao.AccountEntity.Username.As("Username"),
			dao.AccountEntity.Avatar.As("Avatar"),
		).
		Order(dao.EnterprisePersonEntity.ID.Desc()).
		LeftJoin(dao.RoleEntity, dao.RoleEntity.ID.EqCol(dao.EnterprisePersonEntity.RoleID)).
		LeftJoin(dao.AccountEntity, dao.AccountEntity.PersonID.EqCol(dao.EnterprisePersonEntity.ID)).
		ScanByPage(&enterprisePersonBo, offset, limit)
	if err != nil {
		return &types.EnterprisePersonPageResp{
			Data: make([]types.EnterprisePersonModel, 0),
			PageBaseResp: types.PageBaseResp{
				Total:      0,
				PageSize:   req.PageSize,
				PageNumber: req.PageNumber,
			},
		}, nil
	}
	var result = make([]types.EnterprisePersonModel, 0)
	for _, item := range enterprisePersonBo {
		result = append(result, types.EnterprisePersonModel{
			ID:           item.ID,                                 // 主键id
			EnterpriseID: item.EnterpriseID,                       // 关联到enterprise表主键id
			Name:         item.Name,                               // 员工姓名
			Username:     item.Username,                           // 登录账号
			Avatar:       item.Avatar,                             // 头像
			Mobile:       item.Mobile,                             // 员工手机号码
			Email:        item.Email,                              // 员工电邮地址
			RoleID:       item.RoleID,                             // 角色id,关联到role表主键id
			RoleName:     item.RoleName,                           // 角色名称
			Status:       item.Status,                             // 状态,0表示正常,1表示禁用
			CreatedAt:    utils.TimestampToString(item.CreatedAt), // 创建时间
			UpdatedAt:    utils.TimestampToString(item.UpdatedAt), // 更新时间
		})
	}
	return &types.EnterprisePersonPageResp{
		Data: result,
		PageBaseResp: types.PageBaseResp{
			Total:      count,
			PageSize:   req.PageSize,
			PageNumber: req.PageNumber,
		},
	}, nil
}
